import {
	mapState
} from "vuex";
import {
	postFormAction
} from '@/utils/request.js';
const externalEmailMixin = {
	data() {
		return {
			isGet: false,
			queryParam: {
				action: 'search',
				what: '',
				emailId: ''
			},
			// pageSize:9,
			isPostFormAction: true,
			isChecked: false, //长按打开多选
			showMove: false,
			moveOptions: [{
					text: '发件箱',
					value: 3,
				},
				{
					text: '草稿箱',
					value: 0,
				},
				{
					text: '删除',
					value: 2,
				},
			],
			showRead: false,
			readOptions: [{
					text: '已读',
					value: 1,
				},
				{
					text: '未读',
					value: 0,
				},
			],
			type: ''
		}
	},
	onLoad() {
		console.log("this.emailInfo", this.emailInfo)
		if (!this.emailInfo.id) {
			return
		}
		this.queryParam.emailId = this.emailInfo.id
		this.search();
	},
	//下拉刷新
	onPullDownRefresh() {
		// #ifdef MP-WEIXIN
		this.search();
		wx.stopPullDownRefresh();
		// #endif

		// #ifdef APP-ANDROID || H5
		uni.startPullDownRefresh();
		this.search();
		uni.stopPullDownRefresh();
		// #endif

		// #ifdef APP-IOS
		this.search();
		uni.stopPullDownRefresh();
		// #endif
	},
	computed: {
		...mapState(["emailInfo"]),
	},
	methods: {
		_handleRefresh() {
			this.search()
		},
		search() {
			this.page = 1
			this.isChecked = false
			this.getListData();
		},
		formSearch() {
			this.search();
		},
		_handleLook(record) {
			uni.navigateTo({
				url: `/cmn/externalEmail/common/inboxView?${this.type}&mainId=${record.id}`,
			});
		},
		_handleEdit(record) {
			uni.navigateTo({
				url: `/cmn/externalEmail/writebox/index?type=${this.type}&mainId=${record.id}`,
			});
		},
		//转发
		_handleTransmit(record) {
			uni.navigateTo({
				url: `/cmn/externalEmail/writebox/index?type=transmit&mainId=${record.id}`,
			});
		},
		_handleDelete(record) {
			const that = this
			uni.showModal({
				title: "提示",
				content: "您确定删除么？",
				success: (res) => {
					if (res.confirm) {
						that.handleDelete(record.id);
					} else if (res.cancel) {
						console.log("用户点击取消");
					}
				},
			});
		},
		//全选
		_handleSelectAll() {
			this.list.forEach(item => {
				item.checked = true
			})
			this.list = [...this.list]
		},
		//取消全选
		_handleCancel() {
			this.list.forEach(item => {
				item.checked = false
			})
			this.list = [...this.list]
			this.isChecked = false
		},
		//删除
		handleDelete(ids) {
			const that = this
			try {
				uni.showLoading({
					title: '删除中',
					mask: true
				});
				postFormAction(this.url.delete, {
					ids: ids,
					emailId: this.queryParam.emailId
				}).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
					if (res.data.res == 0) {
						//删除后要刷新，确保list能够触底加载
						that._handleRefresh();
					}
				});
			} catch (e) {
				uni.hideLoading()
			} finally {
				uni.hideLoading()
			}
		},
		//批量删除
		_handleDeleteBatch() {
			let selectList = this.list.filter(item => item.checked);
			let checkes = selectList.map(item => item.id);
			if (checkes.length == 0) {
				uni.showToast({
					title: '请选择数据',
					icon: 'none'
				});
				return;
			}
			const ids = checkes.join(',');
			const that = this
			uni.showModal({
				title: "提示",
				content: "您确定删除么？",
				success: async (res) => {
					if (res.confirm) {
						that.handleDelete(ids)
					}
				}
			})
		},
		//置为已读
		async handleSelectRead(index) {
			let selectList = this.list.filter(item => item.checked);
			let checkes = selectList.map(item => item.id);
			if (checkes.length == 0) {
				uni.showToast({
					title: '请选择数据',
					icon: 'none'
				});
				return;
			}
			const ids = checkes.join(',');
			try {
				uni.showLoading({
					title: '操作中...',
					mask: true
				});
				const res = await postFormAction(this.url.setReaded, {
					ids: ids,
					emailId: this.queryParam.emailId,
					isReaded: this.readOptions[index].value
				});
				uni.showToast({
					title: res.msg,
					icon: 'none'
				});
				this._handleRefresh();
			} catch (e) {
				uni.hideLoading()
			} finally {
				uni.hideLoading()
			}
		},
		//移动
		async handleSelectMove(index) {
			console.log("index", index)
			let selectList = this.list.filter(item => item.checked);
			let checkes = selectList.map(item => item.id);
			if (checkes.length == 0) {
				uni.showToast({
					title: '请选择数据',
					icon: 'none'
				});
				return;
			}
			const ids = checkes.join(',');
			try {
				const params = {
					emailId: this.queryParam.emailId,
					ids: ids,
					box: this.moveOptions[index].value,
				};
				uni.showLoading({
					title: '移动中...',
					mask: true
				});
				const res = await postFormAction(this.url.moveMail, params);
				console.log("res", res)
				uni.showToast({
					title: res.msg,
					icon: 'none'
				});
				this._handleRefresh();
			} catch (e) {
				uni.hideLoading()
			} finally {
				uni.hideLoading()
			}
		}
	}
}

export default externalEmailMixin